#include "Cache/Cache.h"
namespace config
{
    const Value<uint64_t> NormalCacheLatency = {"NormalCacheLatency", 3, "Normal Cache Latency"};
} // namespace config

NormalCache::NormalCache()
{
    reset();
}

void NormalCache::run()
{
    while (!ops.empty())
    {
        auto [op, op_cycle] = ops.front();
        if ((op_cycle + latency) <= cycle)
        {
            assert(recv);
            if (!recv->recv(op))
            {
                break;
            }
            ops.pop();
        }
        else
        {
            break;
        }
    }
    has_send = false;
    cycle++;
}
void NormalCache::reset()
{
    has_send = false;
    ops.resize(64);
    latency = config::NormalCacheLatency;
}
bool NormalCache::send(CacheOP *cop)
{
    if(ops.full()) return false;
    if(has_send) return false;
    switch (cop->cmd)
    {
    case CacheReqCmd::LOAD:
    case CacheReqCmd::STORE:
    case CacheReqCmd::MMIO:
    case CacheReqCmd::LR_SC:
    case CacheReqCmd::ATOMICS:
        ops.push({cop, cycle});
        break;
    default:
        break;
    }
    has_send = true;
    SPDLOG_DEBUG("[Cache] Send cache op {} cmd {}",cop->id,cop->cmd);
    return true;
}